package com.maixun.lib_base.ui.fragment

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import com.maixun.lib_base.R
import com.maixun.lib_base.utils.StatusBarUtil

/**
 * @author:Lmoumou
 * @date:2020/7/15
 * 描述:
 **/
abstract class BaseFragmentImpl : Fragment(), BaseFragment {
    //第一次加载
    private var isFirstVisible = true
    protected lateinit var mContext: Context

    override fun onAttach(context: Context) {
        super.onAttach(context)
        this.mContext = context
    }

    override fun onResume() {
        super.onResume()
        initLoadData()
    }

    private fun initLoadData() {
        if (isFirstVisible && isLazyLoad()) {
            loadData()
            isFirstVisible = false
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(getLayoutId(), container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view, savedInstanceState)

        val spaceStatusBar: View? = view.findViewById(R.id.spaceStatusBar)
        initStatusBar(spaceStatusBar, Color.parseColor("#5185F6"))
    }

    override fun initStatusBar(spaceStatusBar: View?, color: Int) {
        spaceStatusBar?.let {
            val lp = it.layoutParams
            lp.height = StatusBarUtil.getStatusBarHeight(mContext)
            it.layoutParams = lp
            it.setBackgroundColor(color)
        }
    }


    override fun openKeyBord(view: View) {
        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(view, 0)
    }

    override fun closeKeyBord(view: View?) {
        val imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        if (view == null) {
            val v = activity?.window?.peekDecorView()
            if (v != null) {
                imm.hideSoftInputFromWindow(v.windowToken, 0)
            }
        } else {
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

}